<template>
  <div>
    <VolHeader style="margin: 30px 0" text="选择汇签人">
      </VolHeader>
      <el-row :gutter="20">
        <el-col :span="6">
          
          <div
        class="first"
        >
        <div class="header">
            <div class="text">公司组织机构</div>
          </div>
          <el-scrollbar height="380px" style="width: 100%">
            <el-tree
              ref="deptree"
              :data="dep_data"
              node-key="id"
              @node-expand="handleNodeClick"
              @node-click="click_handleNodeClick"
            />
          </el-scrollbar>
          </div>
        </el-col>
        <el-col :span="9">
           <div
        class="first"
        >
          <el-table :data="fromtableData" border height="420px">
            <el-table-column prop="username" v-if="false" label="真实姓名">
            </el-table-column>
            <el-table-column prop="usertruename" label="科室人员">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click.prevent="
                    transferRow(scope.$index, fromtableData, scope)
                  "
                >
                  选择 =>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          </div>
        </el-col>
        <el-col :span="9">
          
          <div class="first">
          <el-table :data="toData" border height="420px">
            <el-table-column prop="username" v-if="false" label="真实姓名">
            </el-table-column>
            <el-table-column prop="usertruename" label="汇签人">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click.prevent="deleteRow(scope.$index, toData, scope)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          </div>
        </el-col>
      </el-row>
    
  </div>
</template>
<script>
import http from "@/../src/api/http.js";
import VolHeader from '@/components/basic/VolHeader.vue';
export default {
  components: {
    VolHeader
  },
  data() {
    return {
      act: 0,
      feiy: false,
      cnf:false,
      stepdata: [],
      fromtableData: [],
      toData: [],
      toData_gd:[],
      origin_toData: [], //原始流程处理人，为不让前天删除
      dep_data: [],
      model: {
        //box2: false,
        box3: false,
        box3Row: {},
        xh: "",
      },
      tickno_rows: "", //检查是否重复填写大于"0"重复
    };
  },
  methods: {
    deleteRow(index, rows, scope) {
      rows.splice(index, 1);
    },
    transferRow(index, rows, scope) {
      
      // console.log(scope.row);
      //添加自己不能是汇签人判断：
      let url = "/api/cw_djlc_djzb/checkHuiqianUser?hq_tousername=" + scope.row.username;
      this.http.post(url, {}, true).then((result) => {
        if(result.code == -1){
          this.$message.error(result.message);
        }else{
          rows.splice(index, 1);
          this.toData.push(scope.row);
        }
      })
      /* rows.splice(index, 1);
      this.toData.push(scope.row); */

    },
    get_dep_user_add_table(puser) {
      //加载pdepartid部门下的员工
      this.fromtableData.splice(0, this.fromtableData.length);
      this.$nextTick(() => {
        if (puser.length > 0) {
          for (var i = 0; i <= puser.length - 1; i++) {
            this.fromtableData.push({
              username: puser[i].userName,
              usertruename: puser[i].userTrueName,
            });
          }
        }
      });
    },
    get_dep_fortree(note_type) {
      //加载公司名称
      this.dep_data.splice(0, this.dep_data.length); //清空数组
      this.$nextTick(() => {
        this.$nextTick(() => {
          var turl = "/api/Fk_reim/get_oayi_dep_frotree_oaview?note_type=" + note_type;
          //var turl = "/api/Fk_reim/get_oayi_dep_frotree?note_type=" + note_type;
          this.http.post(turl, {}, true).then((result) => {
            if (note_type == -1) {
              //只加载公司名称
              //console.log(result.subcomp)
              for (let i = 0; i <= result.subcomp.length - 1; i++) {
                let obj = {
                  label: result.subcomp[i].subcompanyname,
                  subcompanyid: result.subcomp[i].company_oa_id,
                  note_type: -1, //-1本节点为根节点，公司级别
                  children: [{}],
                };
                this.dep_data.push(obj); //vue数组赋值push才能更新到界面上=huangxs2021年12月7日 14:44:52
              }
            }
            this.get_dep_user_add_table(result.puser);
          });
        });
      });
    },
    get_yj_dep_fortree(subcompanyid, data, node) {
      //获取一级部门，如信息部、质保、发展部
      const children = node.data.children || node.data;
      this.$nextTick(() => {
        this.$nextTick(() => {
          var turl =
            "/api/Fk_reim/get_oayi_dep_frotree_oaview?note_type=0&subcompanyid=" +
            subcompanyid;
          this.http.post(turl, {}, true).then((result) => {
            //console.log(result.yi_depart)
            for (let i = 0; i <= result.yi_depart.length - 1; i++) {
              if (result.yi_depart[i].depname_oa != "") {
                let obj = {
                  label: result.yi_depart[i].depname_oa,
                  depid_oa: result.yi_depart[i].depid_oa,
                  note_type: 0, //0本节点为一级部门节点
                  children: [{}],
                };

                data.children.push(obj); //vue数组赋值push才能更新到界面上=huangxs2021年12月7日 14:44:52
              }
            }

            this.get_dep_user_add_table(result.puser);
          });
        });
      });
    },
    get_gen_dep_fortree(pdepid, data, node) {
      //获取部门下面科室
      const children = node.data.children || node.data;
      this.$nextTick(() => {
        this.$nextTick(() => {
          var turl =
            "/api/Fk_reim/get_oayi_dep_frotree_oaview?note_type=1&pdepid=" + pdepid;
          this.http.post(turl, {}, true).then((result) => {
            for (let i = 0; i <= result.gen_depart.length - 1; i++) {
              if (result.gen_depart[i].depname_oa != "") {
                var obj;
                if (result.gen_depart[i].child_count == 0) {
                  obj = {
                    label: result.gen_depart[i].depname_oa,
                    depid_oa: result.gen_depart[i].depid_oa,
                    note_type: 0, //1本节点为普通部门节点
                  };
                } else {
                  obj = {
                    label: result.gen_depart[i].depname_oa,
                    depid_oa: result.gen_depart[i].depid_oa,
                    note_type: 0, //1本节点为普通部门节点
                    children: [{}],
                  };
                }

                data.children.push(obj); //vue数组赋值push才能更新到界面上=huangxs2021年12月7日 14:44:52
              }
            }
            //console.log(data)	   ;
            this.get_dep_user_add_table(result.puser);
          });
        });
      });
    },

    
    click_handleNodeClick(data, node) { 
      //
      var turl = "/api/Fk_reim/get_renfordep?pdepid=" + data.depid_oa;
      this.http.post(turl, {}, true).then((result) => {
        console.log(result.puser);
        this.fromtableData.splice(0, this.fromtableData.length);
        this.$nextTick(() => {
          if (result.puser.length > 0) {
            for (var i = 0; i <= result.puser.length - 1; i++) {
              this.fromtableData.push({
                //username: result.puser[i].userName,
                //usertruename: result.puser[i].userTrueName,
                username: result.puser[i].loginid,
                usertruename: result.puser[i].lastname,
              });
            }
          }
        });
      });
    },
    handleNodeClick(data, node) {
      //
      const children = node.data.children;
      children.splice(0, children.length); //先清空所有的子节点
      if (data.note_type == -1) {
        //当前点击的为公司节点，下面需要加载一级部门
        this.get_yj_dep_fortree(data.subcompanyid, data, node);
      }
      if (data.note_type == 0) {
        //当前点击的为部门，下面需要加载子部门
        this.get_gen_dep_fortree(data.depid_oa, data, node);
      }
    },
    open3() {
      console.log("999");
      //this.get_dep_fortree(-1); //-1加载根目录      
  },
  }
};
</script>
<style>
.el-row {
  margin-bottom: 20px;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.header {
  display: flex;
  margin-bottom: 10px;
}

.text {
  line-height: 30px;
  border-bottom: 2px solid #607d8b;
  font-size: 16px;
  /* color: #FF9800; */
  font-weight: 500;
}
.first {
  padding-top: 1px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.sec {
  padding-top: 55px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
</style>
